﻿namespace VectorPicsEditor.Controls
{
    using System;
    using System.ComponentModel;
    using System.Windows.Controls;
    using System.Windows.Forms;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using VectorPicsEditor.Helpers;

    public partial class TextProperties : CommonProperties
    {
        public TextProperties()
        {
            this.InitializeComponent();

            base.topTextBox.TextChanged += this.TopTextBoxTextChanged;
            base.leftTextBox.TextChanged += this.LeftTextBoxTextChanged;
            this.widthTextBox.TextChanged += this.WidthTextBoxTextChanged;
            this.heightTextBox.TextChanged += this.HeightTextBoxTextChanged;
            this.nameTextBox.LostFocus += this.ShapeNameChanged;
        }

        public override void Initialize(ShapeWrapper shape, System.Windows.Forms.ComboBox comboBox)
        {
            base.Initialize(shape, comboBox);
            this.InitFields();
            this.Shape.Shape.SizeChanged += this.TextSizeChanged;
            var topDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.TopProperty, typeof(TextBlock));
            var leftDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.LeftProperty, typeof(TextBlock));
            topDescriptor.AddValueChanged(this.Shape.Shape, this.TextPositionChanged);
            leftDescriptor.AddValueChanged(this.Shape.Shape, this.TextPositionChanged);
        }

        private void InitFields()
        {
            base.topTextBox.Text = Canvas.GetTop(this.Shape.Shape).ToString();
            base.leftTextBox.Text = Canvas.GetLeft(this.Shape.Shape).ToString();
            this.widthTextBox.Text = base.Shape.Shape.Width.ToString();
            this.heightTextBox.Text = base.Shape.Shape.Height.ToString();
            base.nameTextBox.Text = base.Shape.Name;
            this.opacityTrackBar.Value = (int)(this.Shape.Shape.Opacity * 100);
            this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            this.sizeComboBox.Text = (this.Shape.Shape as TextBlock).FontSize.ToString();
            this.textColorPanel.BackColor = Converter.ConvertColor(((this.Shape.Shape as TextBlock).Foreground as SolidColorBrush).Color);
            this.backgroundColorPanel.BackgroundImage = VectorPicsEditor.Properties.Resources.transparent;

            //initialize font dropdown
            foreach (FontFamily font in System.Windows.Media.Fonts.SystemFontFamilies)
            {
                ComboboxItem item = new ComboboxItem();
                item.Text = font.Source;
                item.Value = font;
                fontComboBox.Items.Add(item);
            }
            ComboboxItem cur = new ComboboxItem();
            cur.Text = (this.Shape.Shape as TextBlock).FontFamily.Source;
            cur.Value = (this.Shape.Shape as TextBlock).FontFamily;
            if(!fontComboBox.Items.Contains(cur))
                fontComboBox.Items.Add(cur);
            fontComboBox.SelectedItem = cur;

            //initialize font weight combobox

            this.fontWeightComboBox.Items.Add(new ComboboxItem("Normal", System.Windows.FontWeights.Normal));
            this.fontWeightComboBox.Items.Add(new ComboboxItem("Bold", System.Windows.FontWeights.Bold));
            if ((this.Shape.Shape as TextBlock).FontWeight == System.Windows.FontWeights.Normal)
                fontWeightComboBox.SelectedIndex = 0;
            else
                fontWeightComboBox.SelectedIndex = 1;

            //initialize font style combobox

            this.fontStyleConmoBox.Items.Add(new ComboboxItem("Normal", System.Windows.FontStyles.Normal));
            this.fontStyleConmoBox.Items.Add(new ComboboxItem("Italic", System.Windows.FontStyles.Italic));
            this.fontStyleConmoBox.Items.Add(new ComboboxItem("Oblique", System.Windows.FontStyles.Oblique));
            if ((this.Shape.Shape as TextBlock).FontStyle == System.Windows.FontStyles.Normal)
                this.fontStyleConmoBox.SelectedIndex = 0;
            else if ((this.Shape.Shape as TextBlock).FontStyle == System.Windows.FontStyles.Italic)
                this.fontStyleConmoBox.SelectedIndex = 1;
            else
                this.fontStyleConmoBox.SelectedIndex = 2;
        }

        private void TextPositionChanged(object sender, EventArgs e)
        {
            base.topTextBox.Text = Canvas.GetTop(this.Shape.Shape).ToString();
            base.leftTextBox.Text = Canvas.GetLeft(this.Shape.Shape).ToString();
            this.Shape.WasChanged = true;
        }

        void ShapeNameChanged(object sender, EventArgs e)
        {
            if (this.Shape.Name != nameTextBox.Text)
            {
                this.Shape.Name = nameTextBox.Text;
                this.Shape.WasChanged = true;
                int index = this.ShapesComboBox.SelectedIndex;
                this.ShapesComboBox.Items[index] = this.Shape.Name;
            }
        }

        void LeftTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(base.leftTextBox.Text);
                if (x != Canvas.GetLeft(this.Shape.Shape))
                {
                    this.Shape.Shape.SetValue(Canvas.LeftProperty, x);
                }
            }
            catch (FormatException)
            {
                this.TextPositionChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        void TopTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double y = double.Parse(base.topTextBox.Text);
                if (y != Canvas.GetLeft(this.Shape.Shape))
                {
                    this.Shape.Shape.SetValue(Canvas.TopProperty, y);
                }
            }
            catch (FormatException)
            {
                this.TextPositionChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeBackGroundColor(object sender, EventArgs e)
        {
            var newColor = base.OpenColorDialog();
            if (newColor != null)
            {
                this.backgroundColorPanel.BackgroundImage = null;
                this.backgroundColorPanel.BackColor = newColor.Value;
                (this.Shape.Shape as TextBlock).Background = new SolidColorBrush(Converter.ConvertColor(newColor.Value));
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeOpacity(object sender, EventArgs e)
        {
            this.Shape.Shape.Opacity = this.opacityTrackBar.Value / 100f;
            this.Shape.WasChanged = true;
        }

        private void ChangeZindex(object sender, EventArgs e)
        {
            try
            {
                int c = int.Parse(this.zindexTextBox.Text);
                Canvas.SetZIndex(this.Shape.Shape, c);
            }
            catch (FormatException)
            {
                this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void backColorTransperentButton_Click(object sender, EventArgs e)
        {
            this.backgroundColorPanel.BackgroundImage = VectorPicsEditor.Properties.Resources.transparent;
            (this.Shape.Shape as TextBlock).Background = new SolidColorBrush(Colors.Transparent);
            this.Shape.WasChanged = true;
        }

        void HeightTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.heightTextBox.Text);
                if (x != base.Shape.Shape.Height)
                {
                    base.Shape.Shape.Height = x;
                }
            }
            catch (FormatException)
            {
                this.TextSizeChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        void WidthTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.widthTextBox.Text);
                if (x != base.Shape.Shape.Width)
                {
                    this.Shape.Shape.Width = x;
                }
            }
            catch (FormatException)
            {
                this.TextSizeChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void TextSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            this.widthTextBox.Text = base.Shape.Shape.Width.ToString();
            this.heightTextBox.Text = base.Shape.Shape.Height.ToString();
            this.Shape.WasChanged = true;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            //TODO: remove all events related to this shape
            this.Shape.Remove(this.Shape.Shape);
            int index = this.ShapesComboBox.SelectedIndex;
            if (index != 0)
                this.ShapesComboBox.SelectedIndex = index - 1;
            else
                this.Dispose();
            this.ShapesComboBox.Items.RemoveAt(index);
        }

        private void TextChanged(object sender, EventArgs e)
        {
            this.Shape.Shape.Opacity = this.opacityTrackBar.Value / 100f;
            this.Shape.WasChanged = true;
        }

        private void editText_Click(object sender, EventArgs e)
        {
            this.Shape.TextBlockEdit();
        }

        private void sizeComboBox_TextUpdate(object sender, EventArgs e)
        {
            double val = 0;
            if(!double.TryParse(sizeComboBox.Text,out val))
            {
                sizeComboBox.Text = "11";
                val = 11;
            }
            (this.Shape.Shape as TextBlock).FontSize = val;
            this.Shape.WasChanged = true;
        }

        private void sizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            (this.Shape.Shape as TextBlock).FontSize = double.Parse(sizeComboBox.Text);
            this.Shape.WasChanged = true;
        }

        private void fontComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            (this.Shape.Shape as TextBlock).FontFamily = (fontComboBox.SelectedItem as ComboboxItem).Value as FontFamily;
            this.Shape.WasChanged = true;
        }

        private void fontWeightComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Windows.FontWeight? weight = (fontWeightComboBox.SelectedItem as ComboboxItem).Value as System.Windows.FontWeight?;
            (this.Shape.Shape as TextBlock).FontWeight = weight ?? System.Windows.FontWeights.Normal;
            this.Shape.WasChanged = true;
        }

        private void fontStyleConmoBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Windows.FontStyle? style = (fontStyleConmoBox.SelectedItem as ComboboxItem).Value as System.Windows.FontStyle?;
            (this.Shape.Shape as TextBlock).FontStyle = style ?? System.Windows.FontStyles.Normal;
            this.Shape.WasChanged = true;
        }

        private void textColorChangeButton_Click(object sender, EventArgs e)
        {
            var newColor = base.OpenColorDialog();
            if (newColor != null)
            {
                this.textColorPanel.BackgroundImage = null;
                this.textColorPanel.BackColor = newColor.Value;
                (this.Shape.Shape as TextBlock).Foreground = new SolidColorBrush(Converter.ConvertColor(newColor.Value));
                this.Shape.WasChanged = true;
            }
        }
    }
}
